using System;
using System.Xml.Serialization;
using System.Globalization;

namespace org.nativeinstruments.nml
{
	
	public class LOUDNESS
	{
		// ATTRIBUTES
        [XmlIgnore]
        public double? PEAK_DB { get; set; }
        [XmlAttribute("PEAK_DB")]
        public string PEAK_DB_RAW
        {
            get { return toString(PEAK_DB); }
            set { PEAK_DB = parse(value); }
        }

        [XmlIgnore]
        public double? PERCEIVED_DB { get; set; }
        [XmlAttribute("PERCEIVED_DB")]
		public string PERCEIVED_DB_RAW
        {
            get { return toString(PERCEIVED_DB); }
            set { PERCEIVED_DB = parse(value); }
        }

        [XmlIgnore]
        public double? ANALYZED_DB { get; set; }
        [XmlAttribute("ANALYZED_DB")]
		public string ANALYZED_DB_RAW
        {
            get { return toString(ANALYZED_DB); }
            set { ANALYZED_DB = parse(value); }
        }
		
		// ELEMENTS
		[XmlText]
		public string Value { get; set; }
		
		// CONSTRUCTOR
		public LOUDNESS()
		{}


        private string toString(double? value)
        {
            if (value == null) return string.Empty;
            else if (value == double.PositiveInfinity) return "inf";
            else return value.Value.ToString(CultureInfo.InvariantCulture);
        }

        private double? parse(string value)
        {
            if (String.IsNullOrWhiteSpace(value)) return null;
            else if (value == "inf") return double.PositiveInfinity;
            else return double.Parse(value, CultureInfo.InvariantCulture);
        }
	}
}
